package edu.spangler.dang.FriendTracker.provider;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Provides access to the Friends database.
 */

class FriendsDatabase extends SQLiteOpenHelper
{
    public static final int Version = 1;
    public static final String Name = "Friends";
    public static final String FriendsTable = "friends";

    /**
     * Creates a new Friends database.
     * @param context The Context used to open or create the database.
     */
    
    public FriendsDatabase(Context context)
    {
        super(context, Name, null, Version);
    }

    /**
     * Called when the database is created for the first time.
     * This is where the creation of tables and the initial population of the tables should happen.
     * @param sqLiteDatabase The database.
     */
    
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase)
    {
        sqLiteDatabase.execSQL("CREATE TABLE " + FriendsTable + " ("
                + Friends._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + Friends.Name + " TEXT NOT NULL, "
                + Friends.Latitude + " REAL NOT NULL, "
                + Friends.Longitude + " REAL NOT NULL);");
    }

    /**
     * Called when the database needs to be upgraded.
     * The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.
     * @param sqLiteDatabase The database.
     * @param oldVersion The old database version.
     * @param newVersion The new database version.
     */

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion)
    {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + FriendsTable + ";");
        onCreate(sqLiteDatabase);
    }
}
